﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.LLBLGen.Pro.ORMSupportClasses;
using CARMSBusiness.InterFace;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;
using CARMSData.Linq;

namespace CARMSBusiness
{
    public class SecurityBusiness
    {

        #region
        //public static Guid? GetEmployeeIDByUserID(Guid userID)
        //{
        //    var user = new UserEntity(userID);
        //    if (!user.IsNew)
        //    {
        //        if (user.EmployeeId.HasValue)
        //        {
        //            return user.EmployeeId.Value;
        //        }
        //    }
        //    return null;

        //}


        //public static string GetEmployeeNameByUserID(Guid userID)
        //{
        //    var user = new UserEntity(userID);
        //    if (!user.IsNew)
        //    {
        //        if (user.EmployeeId.HasValue)
        //        {
        //            var emp = new EmployeeEntity(user.EmployeeId.Value);
        //            if(!emp.IsNew)
        //            {
        //                return emp.FirstName + " " + emp.LastName;
        //            }
        //        }
        //        return user.FullName;
        //    }
        //    return string.Empty;

        //}

        //public static EmployeeCollection GetEmployeeByRoleID(Guid roleID)
        //{
        //    //选取user中的employee id
        //    var userCollection = new UserCollection();
        //    userCollection.GetMulti(UserRoleFields.RoleId == roleID,
        //                            new RelationCollection {UserEntity.Relations.UserRoleEntityUsingUserId});

        //    var empList = from item in userCollection where item.EmployeeId != null select item.EmployeeId;

        //    var empCompare = new FieldCompareRangePredicate(EmployeeFields.EmployeeId, false, empList.ToArray());
        //    var collection = new EmployeeCollection();
        //    collection.GetMulti(empCompare);
        //    return collection;

        //}
        #endregion

        #region user password

        public static UserEntity GetUserDataByID(int strUserID)
        {
            var userEntity = new UserEntity(strUserID);
            if (!userEntity.IsNew)
            {
                return userEntity;
            }

            return null;
        }

        public static UserCollection GetUserList()
        {
            var userCollection = new UserCollection();
            userCollection.GetMulti(null);
            return userCollection;
        }

        public static bool CheckUserLoginNameExist(UserEntity data)
        {
            var collection = new UserCollection();
            var sameCount = collection.GetDbCount(UserFields.UserId != data.UserId &
                                   UserFields.LoginName == data.LoginName.Trim() &
                                   UserFields.IsActived == true);
            return sameCount > 0;
        }


        public static UserEntity ValidateUser(string strLoginName, string password, string sessionID, string ipaddress)
        {
            var userCollection = new UserCollection();
            userCollection.GetMulti(UserFields.LoginName == strLoginName &
                                    UserFields.Password ==password & UserFields.IsActived == true);

            var entity = userCollection.FirstOrDefault();
            if (entity != null)
            {
                LogBusiness.AddLog(new LogEntity
                {
                    Category = LogCategories.Security, 
                    Source = "Login",
                    UserName = strLoginName,
                    Message =
                        "Logon successful." + Environment.NewLine +
                        "Logon account: " + strLoginName + Environment.NewLine,
                    SessionId = sessionID,
                    VistorIp = ipaddress,
                });
                return entity;
            }
            else
            {
                LogBusiness.AddLog(new LogEntity
                {
                    Category = LogCategories.Security,
                     
                    Source = "Login",
                    UserName = strLoginName,
                    Message =
                        "Logon failed." + Environment.NewLine +
                        "Logon account: " + strLoginName + Environment.NewLine,
                    SessionId = sessionID,
                    VistorIp = ipaddress,
                });
                return null;
            }
        }


        public static UserCollection GetUserList(PredicateExpression condition)
        {
            var collection = new UserCollection();
            collection.GetMulti(condition);
            return collection;
        }

        public static UserCollection GetUserList(PredicateExpression Condition, SortExpression sort, int pageSize, int pageIndex)
        {
            var collection = new UserCollection();
            collection.GetMulti(Condition, 0, sort, null, pageIndex, pageSize);
            return collection;
        }

        public static UserEntity GetUserByID(int userID)
        {
            var userEntity = new UserEntity(userID);
            if (!userEntity.IsNew)
            {
                return userEntity;
            }
            return null;
        }


        public static bool ChangePassword(int userID, string strPassword)
        {
            var userEntity = new UserEntity(userID);
            if (!userEntity.IsNew)
            {
                userEntity.Password =strPassword.Trim();
                userEntity.Save();
                return true;
            }
            return false;
        }


        public static bool DeleteUser(int strUserID)
        {
            var userEntity = new UserEntity(strUserID);
            if (!userEntity.IsNew)
            {
                userEntity.IsActived = false;
                return userEntity.Save();
            }
            return false;
        }


        public static bool SaveUser(UserEntity userEntity)
        {
            return userEntity.Save();
        }


        public static bool SaveUser(UserEntity userEntity, List<int> roleList)
        {
            if (userEntity.Save())
            {
                var usreToRoleCollection = new UserRoleCollection();
                usreToRoleCollection.DeleteMulti(UserRoleFields.UserId == userEntity.UserId);
                foreach (var roleID in roleList)
                {
                    var userToRole = new UserRoleEntity
                                         {
                                             RoleId = roleID,
                                             UserId = userEntity.UserId
                                         };
                    userToRole.Save();
                }
                return true;
            }
            return false;
        }


        #endregion


        #region role data foundation

        public static RoleCollection GetRoleList()
        {
            var collection = new RoleCollection();
            collection.GetMulti(null);
            return collection;
        }

        public static RoleCollection GetRoleListByUserID(int userID)
        {
            var collection = new RoleCollection();
            collection.GetMulti(UserRoleFields.UserId == userID, new RelationCollection { RoleEntity.Relations.UserRoleEntityUsingRoleId });
            return collection;
        }
        #endregion
    }
}
